package com.g2room.myuall.grap.ky;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.myuall.grap.ky.model.po.PeeMajor;
import com.g2room.myuall.grap.ky.model.po.PeeUnivMajor;
import com.g2room.myuall.util.DbUtil;

public class GrapMajor {
	public static void main(String[] args) throws Exception{
		new GrapMajor().getMajors();
		// http://www.kaoyan.com/kaoyan/19/33736/==081303
//		http://www.kaoyan.com/kaoyan/01/34460/==097200
//		new GrapMajor().getUnivMajors("http://www.kaoyan.com/kaoyan/19/33805", "071100");
	}
	
	private void getMajors() {
		String url = "C:/Users/butterfly/Desktop/t.html";

		Document doc = null;
		try {
			doc = GenericGrap.getDocByLocal(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Element divEl = doc.getElementById("specialRight");
		Elements childEls = divEl.children();
		int i = 0;
		List<PeeMajor> pms = new ArrayList<PeeMajor>();
		Map<String, String> majorUrls = new HashMap<String, String>();
		String catCode = "";
		String catName = "";
		String typeCode = "";
		String typeName = "";
		for(Element el : childEls) {
			if(i<2) {i++; continue;}
			// 判断是不是一级
//			if(n.attr("class").equalsIgnoreCase("tlTitle"))
			
			if(el.hasAttr("id")) {
				if(el.text().equals("01哲学")) {
					catCode = "01";
					catName = "哲学";
//					pm.setCategoryCode("01");
//					pm.setCategoryName("哲学");
				} else {
					String[] cat = el.text().split(" ");
//					pm.setCategoryCode(cat[0]);
//					pm.setCategoryName(cat[1]);
					catCode = cat[0];
					catName = cat[1];
				}
				typeCode = null;
				typeName =null;
			} else if(el.attr("class").equals("xkTitle")){
				/**
				 <p class="xkTitle"><span>0201</span>理论经济学</p>
				 <a href="http://www.kaoyan.com/kaoyan/21/550841/" target="_blank" title="资产评估★"><span>0256</span>资产评估★</a>
				 */
				Element spanEl = el.getElementsByTag("span").first();
				typeCode= spanEl.text();
				typeName = el.text().replace(typeCode, "");
				if(typeCode.equals("1202")) {
					System.out.println(el.html());
				}
//				pm.setTypeCode(spanEl.text());
//				pm.setTypeName(el.text());
				Elements aels  = el.getElementsByTag("a");
				if(null != aels && !aels.isEmpty()) {
					majorUrls.put(aels.first().attr("href"), typeCode);
					PeeMajor pm = new  PeeMajor();
					pm.setCategoryCode(catCode);
					pm.setCategoryName(catName);
					pm.setTypeCode(typeCode);
					pm.setTypeName(typeName);
					pms.add(pm);
				}
			} else if(el.attr("class").equals("xkList")){
				/**
				 <a href="http://www.kaoyan.com/kaoyan/20/34021/" target="_blank" title="哲学"><span>010100</span>哲学</a>
				 */
				Elements aEls = el.getElementsByTag("a");
				for(Element aEl : aEls) {
					Elements spanEls = aEl.getElementsByTag("span");
					if(null == spanEls || spanEls.isEmpty()) {
						System.out.println(aEl.html());
						continue;
					}
					PeeMajor pm = new  PeeMajor();
					pm.setCode(spanEls.first().text());
					pm.setName(aEl.text().replace(pm.getCode(), ""));
					
					pm.setName(aEl.text().replace(pm.getCode(), ""));
					majorUrls.put(aEl.attr("href"), pm.getCode());
					pm.setCategoryCode(catCode);
					pm.setCategoryName(catName);
					pm.setTypeCode(typeCode);
					pm.setTypeName(typeName);
					pms.add(pm);
				}
			}
		}
//		try {
//			inser2MajorDb(pms);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
		
		for(String majorUrl : majorUrls.keySet()) {
			getUnivMajors(majorUrl, majorUrls.get(majorUrl));
		}
	}
	

	private void getUnivMajors(String url, String majorCode) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Elements trEls = null;
		try {
			trEls = doc.getElementsByTag("table").get(1).getElementsByTag("tr");
		} catch (Exception e) {
			System.out.println(url + "==" + majorCode);
		}
		if(null == trEls) return;
		int i = 0;
		List<PeeUnivMajor> pums = new ArrayList<PeeUnivMajor>();
		String prov = "";
		for(Element trEl : trEls) {
			if(i<2) {i++;continue;}
			Elements strongEls = trEl.getElementsByTag("strong");
			if(null != strongEls &&  !strongEls.isEmpty()) {
				prov = strongEls.first().text();
			} else {
				Elements tdEls = trEl.getElementsByTag("td");
				for(Element tdEl : tdEls) {
					PeeUnivMajor pum = new  PeeUnivMajor();
					// <a href="http://yz.kaoyan.com/cipe/">首都体育学院</a>
					Elements aEl = tdEl.getElementsByTag("a");
					if(null != aEl && !aEl.isEmpty()) {
						String href = aEl.attr("href");
						pum.setProvinceName(prov);
						pum.setSchoolCode(href.replaceAll("http://yz.kaoyan.com/", "/").replace("/", ""));
					}
					
					pum.setSchoolName(tdEl.text());
					pum.setMajorCode(majorCode);
					if(StringUtil.isNotBlank(pum.getSchoolName()) && pum.getSchoolName().length() > 1) {
						pums.add(pum);
					}
				}
			}
		}
		try {
			inser2UnivMajorDb(pums);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void inser2MajorDb(List<PeeMajor> pms) throws SQLException {
		String sql = "insert into pee_major(type_code,code,category_name,type_name,category_code,name) values(?,?,?,?,?,?)";
		PreparedStatement ps = DbUtil.getConn().prepareStatement(sql);
		for(PeeMajor pm : pms) {
			ps.setString(1, pm.getTypeCode());
			ps.setString(2, pm.getCode());
			ps.setString(3, pm.getCategoryName());
			ps.setString(4, pm.getTypeName());
			ps.setString(5, pm.getCategoryCode());
			ps.setString(6, pm.getName());
			ps.addBatch();
		}
		ps.executeBatch();
	}
	

	private void inser2UnivMajorDb(List<PeeUnivMajor> pums) throws SQLException {
		String sql = "insert into pee_univ_major(major_name,province_name,major_code,school_code,school_name) values(?,?,?,?,?)";
		PreparedStatement ps = DbUtil.getConn().prepareStatement(sql);
		for(PeeUnivMajor pum : pums) {
			ps.setString(1, pum.getMajorName());
			ps.setString(2, pum.getProvinceName());
			ps.setString(3, pum.getMajorCode());
			ps.setString(4, pum.getSchoolCode());
			ps.setString(5, pum.getSchoolName());
			ps.addBatch();
		}
		ps.executeBatch();
	}
}
